home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / print / gs261sr4.zip / PS2EPSI.PS < prev    next >
Text File  |  1993-05-21  |  6KB  |  193 lines

  1. %    Copyright (C) 1990, 1991, 1993 Aladdin Enterprises.  All rights reserved.
  2. %
  3. % This file is part of Ghostscript.
  4. %
  5. % Ghostscript is distributed in the hope that it will be useful, but
  6. % WITHOUT ANY WARRANTY.  No author or distributor accepts responsibility
  7. % to anyone for the consequences of using it or for whether it serves any
  8. % particular purpose or works at all, unless he says so in writing.  Refer
  9. % to the Ghostscript General Public License for full details.
  10. %
  11. % Everyone is granted permission to copy, modify and redistribute
  12. % Ghostscript, but only under the conditions described in the Ghostscript
  13. % General Public License.  A copy of this license is supposed to have been
  14. % given to you along with Ghostscript so you can know your rights and
  15. % responsibilities.  It should be in a file named COPYING.  Among other
  16. % things, the copyright notice and this notice must be preserved on all
  17. % copies.
  18.  
  19. % Convert an arbitrary PostScript file to an EPSI file.
  20. %
  21. %    NOTE: this file was contributed by a user: please contact
  22. %    George Cameron <george@bio-medical-physics.aberdeen.ac.uk>
  23. %    if you have questions.
  24. %
  25.  
  26. % Initialize, and redefine copypage and showpage.
  27. /ps2edict 25 dict def
  28. ps2edict begin
  29.  
  30.                 % The main procedure
  31.   /ps2epsi
  32.    {                % Open the file
  33.      outfile (w) file /epsifile exch def
  34.                     % Get the device parameters
  35.      currentdevice getdeviceprops .dicttomark
  36.      dup /HWSize get aload pop
  37.        /devheight exch def
  38.        /devwidth exch def
  39.      /InitialMatrix get
  40.        /devmatrix exch def
  41.                 % Make a corresponding memory device
  42.      devmatrix devwidth devheight <ff 00>
  43.      makeimagedevice
  44.      /arraydevice exch def
  45.      arraydevice setdevice    % (does an erasepage)
  46.      /rowwidth devwidth 7 add 8 idiv def
  47.      /row rowwidth string def
  48.                 % Replace the definition of showpage
  49.      userdict /showpage { ps2edict begin epsipage end } bind put
  50.    } def
  51.  
  52.  /margintest
  53.  {
  54.       { dup arraydevice exch row copyscanlines pop
  55.     -1 0 1 rowwidth 1 sub
  56.         { dup row exch get 0 ne { exit }{ pop } ifelse
  57.         } for
  58.     dup -1 ne { exch pop } if
  59.     -1 ne { exit } if pop
  60.       } for
  61.  } bind def
  62.  
  63.   /epsipage
  64.    {
  65.      /loopcount devheight 1 sub def
  66.  
  67.      % Find top margin
  68.      -1 0 1 loopcount margintest
  69.      dup -1 eq { (blank page!!\n) print 0 .quit }{ exch pop } ifelse 
  70.      /tm exch def
  71.  
  72.      % Find bottom margin
  73.      loopcount -1 0 margintest
  74.      /bm exch def
  75.      
  76.      % Initialise limit variables
  77.      /loopcount rowwidth 1 sub def
  78.      /lm loopcount def /lmb 0 def
  79.      /rm 0 def /rmb 0 def
  80.  
  81.      % Find left and right boundaries of image
  82.      tm 1 bm
  83.       { % Get more data
  84.         arraydevice exch row copyscanlines pop
  85.         % Scan from left to find first non-zero element
  86.         % We save first the element, then the index
  87.     -1 0 1 loopcount
  88.         { dup row exch get dup 0 ne { exch exit }{ pop pop } ifelse
  89.         } for
  90.         % If we found -1, row is blank ..
  91.     dup -1 ne 
  92.         { % Find the leftmost index
  93.           dup lm lt
  94.           % If the new index is less, we save index and element
  95.           { /lm exch def /lmb exch def }
  96.           % If the index is equal, we or the bits together
  97.           { lm eq { lmb or /lmb exch def }{ pop } ifelse
  98.           } ifelse
  99.       % Now find the rightmost index
  100.       loopcount -1 0
  101.           { dup row exch get dup 0 ne { exch exit }{ pop pop } ifelse
  102.           } for
  103.       dup rm gt
  104.           % If the new index is greater, we save index and element
  105.           { /rm exch def /rmb exch def }
  106.           % If the index is equal, or the bits
  107.           { rm eq { rmb or /rmb exch def } { pop } ifelse
  108.           } ifelse
  109.         } if
  110.     pop
  111.       } for
  112.  
  113.      % Now we find the real left & right bit positions
  114.      256 0 1 7
  115.      { exch 2 div dup lmb le { pop exit }{ exch pop } ifelse
  116.      } for
  117.      /lmb exch def
  118.  
  119.      1 7 -1 0
  120.      { exch dup dup rmb and eq { pop exit }{ 2 mul exch pop } ifelse
  121.      } for
  122.      /rmb exch def
  123.  
  124.      % Calculate the bounding box values
  125.      /llx lm 8 mul lmb add def
  126.      /lly devheight bm sub def
  127.      /urx rm 8 mul rmb add def
  128.      /ury devheight tm sub def
  129.  
  130.      % Write out the magic string and bounding box information
  131.      epsifile (%!PS-Adobe-2.0 EPSF-1.2\n) writestring
  132.      epsifile (%%BoundingBox: ) writestring
  133.      epsifile llx write==only epsifile ( ) writestring
  134.      epsifile lly write==only epsifile ( ) writestring
  135.      epsifile urx write==only epsifile ( ) writestring
  136.      epsifile ury write==
  137.      epsifile (%%BeginPreview: ) writestring
  138.      epsifile urx llx sub 1 add write==only epsifile ( ) writestring
  139.      epsifile bm tm sub 1 add write==only epsifile ( 1 ) writestring
  140.      epsifile bm tm sub 1 add write==
  141.  
  142.      % Define character and bit widths for the output line buffer
  143.      /cwidth rm lm sub 1 add 8 mul 7 add 8 idiv def
  144.      /bwidth cwidth 8 mul def
  145.      /owidth urx llx sub 1 add 7 add 8 idiv def
  146.      /out cwidth string def
  147.  
  148.      % Create a 1-bit-high device for bitblt to align with the bbox
  149.      matrix cwidth 8 mul 1 <00 ff> makeimagedevice 
  150.      /linedevice exch def
  151.  
  152.      % 'image' a zero string to clear the line device
  153.      bwidth 1 1 matrix cwidth string linedevice setdevice image
  154.      arraydevice setdevice
  155.  
  156.      tm 1 bm
  157.       { % Get a scan line interval from the array device
  158.     arraydevice exch row copyscanlines lm cwidth getinterval
  159.     lmb 0 gt
  160.     { % 'image' it into the line device with the lmb offset
  161.       bwidth 1 1 [1 0 0 1 lmb 0] 5 -1 roll linedevice setdevice image
  162.       % Now we get the modified scan line
  163.       linedevice 0 out copyscanlines 0 owidth getinterval
  164.       % Reset the current device
  165.       arraydevice setdevice
  166.     } if
  167.     % Write out the hex data
  168.         epsifile (% ) writestring 
  169.         epsifile exch writehexstring
  170.     epsifile (\n) writestring
  171.       } for
  172.  
  173.      epsifile (%%EndImage\n) writestring
  174.      epsifile (%%EndPreview\n) writestring
  175.      epsifile flushfile
  176.      erasepage initgraphics
  177.  
  178.      DonePage 0 1 put
  179.    } bind def
  180.  
  181.  
  182. (outfile) getenv
  183.   { /outfile exch def 
  184.     ps2epsi
  185.  
  186.     /DonePage 1 string def
  187.     (%stdin) (r) file cvx execute0
  188.     DonePage 0 get 0 eq { showpage } if
  189.   } if
  190.  
  191. end
  192. quit
  193.